import globalSettings from '@/settings'

const {
  productName,
  version
} = globalSettings

const storageSetting = JSON.parse(localStorage.getItem('app-settings')) || ''
const state = {
  productName: storageSetting.productName === undefined ? productName : storageSetting.productName,
  version: storageSetting.version === undefined ? version : storageSetting.version,
}

const mutations = {
  CHANGE_SETTING: (state, {
    key,
    value
  }) => {
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
  }
}

const actions = {
  // 修改设置
  changeSetting({
    commit
  }, data) {
    commit('CHANGE_SETTING', data)
  },
  // 设置产品标题
  setProductName({ commit }, productName) {
    state.productName = productName
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
